package org.eae.eldf.webapp.controller.module;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eae.eldf.webapp.base.AbstractController;
import org.eae.eldf.webapp.common.bean.Result;
import org.eae.eldf.webapp.common.constant.StateDefine;
import org.eae.eldf.webapp.vo.CheckboxZtreeVo;
import org.eae.eldf.webapp.vo.RolePurviewZtreeVo;
import org.apache.commons.lang.StringUtils;
import org.eae.eldf.dto.module.ModuleRolePurviewDto;
import org.eae.eldf.entity.module.ModuleMenu;
import org.eae.eldf.entity.module.ModulePurview;
import org.eae.eldf.entity.module.ModuleRole;
import org.eae.eldf.entity.module.ModuleRolePurview;
import org.eae.eldf.entity.sys.SysMenu;
import org.eae.eldf.entity.sys.SysPurview;
import org.eae.eldf.framework.json.JSONArray;
import org.eae.eldf.framework.persistence.Criteria;
import org.eae.eldf.framework.spring.SpringSecurityUtils;
import org.eae.eldf.service.module.ModuleMenuService;
import org.eae.eldf.service.module.ModulePurviewService;
import org.eae.eldf.service.module.ModuleRolePurviewService;
import org.eae.eldf.service.module.ModuleRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/module")
public class ModuleRolePurviewController extends AbstractController<ModuleRolePurview> {

	private ModuleRoleService moduleRoleService;

	private ModuleMenuService moduleMenuService;

	private ModulePurviewService modulePurviewService;
	
	private ModuleRolePurviewService moduleRolePurviewService;
	
	@Autowired
	public void setModulePurviewService(ModulePurviewService modulePurviewService) {
		this.modulePurviewService = modulePurviewService;
	}

	@Autowired
	public void setModuleRoleService(ModuleRoleService moduleRoleService) {
		this.moduleRoleService = moduleRoleService;
	}

	@Autowired
	public void setModuleMenuService(ModuleMenuService moduleMenuService) {
		this.moduleMenuService = moduleMenuService;
	}

	@Autowired
	public void setModuleRolePurviewService(ModuleRolePurviewService moduleRolePurviewService) {
		this.moduleRolePurviewService = moduleRolePurviewService;
	}

	@RequestMapping(value = "/role/purview")
	@ResponseBody
	public RolePurviewZtreeVo getRolePurviewTree(@RequestParam(required = false) Long id, ModelMap model,
			HttpServletRequest request, HttpServletResponse response) {
		ModuleRole role = moduleRoleService.get(ModuleRole.class, id);
		RolePurviewZtreeVo v = new RolePurviewZtreeVo();
		// 通过角色模块查询角色模块菜单
		List<ModuleMenu> menuList = moduleMenuService.queryList(Criteria.create(ModuleMenu.class).where("moduleInfoId",
				new Object[] { role.getModuleInfoId() }));
		//查询菜单权限
		List<ModulePurview> purList = modulePurviewService.queryByMenuList(menuList);
		
		//查询对应角色权限
		List<ModuleRolePurview> purRoleList = moduleRolePurviewService.queryList(Criteria.create(
				ModuleRolePurview.class).where("moduleRoleId", new Object[] { id }));
		// 构造tree
		Map<String, CheckboxZtreeVo> roleMap=new LinkedHashMap<String, CheckboxZtreeVo>();
		List<Long> purids = new ArrayList<Long>();
		if (purRoleList != null) {
			for (ModuleRolePurview purview2 : purRoleList) {
				purids.add(purview2.getModulePurviewId());
			}
		}
		
		//菜单加入到treeMap 加menu_避免id重复
    	for(ModuleMenu menu:menuList){
    		//RolePurviewCheckDto rolePurviewCheckDto=new RolePurviewCheckDto();
    		CheckboxZtreeVo vo = new CheckboxZtreeVo();
    		vo.setpId(menu.getParentId()!=0?"menu_"+menu.getParentId().toString():null);
    		vo.setName(menu.getMenuName());
    		vo.setId(null);
    		vo.setLeaf(false);
    		
    		
    		roleMap.put("menu_"+menu.getModuleMenuId().toString(), vo);
    	}
    		
    		for (ModulePurview purview : purList) {
    			CheckboxZtreeVo tree = new CheckboxZtreeVo();
    			tree.setId(purview.getModulePurviewId());
    			tree.setName(purview.getPurName());
    			tree.setpId("menu_"+purview.getModuleMenuId());
    			tree.setLeaf(true);
    			roleMap.put(purview.getModulePurviewId().toString(), tree);
    		}
    		
    		
    	
    	CheckboxZtreeVo root = new CheckboxZtreeVo();
		// 递归构造树
		Iterator<Map.Entry<String, CheckboxZtreeVo>> it = roleMap.entrySet()
				.iterator();
		while (it.hasNext()) {
			Map.Entry<String, CheckboxZtreeVo> entry = it.next();
			CheckboxZtreeVo tree = entry.getValue();
			if (StringUtils.isBlank(tree.getpId())) {// 如果是根节点
				root.getChildren().add(tree);
			} else {
				CheckboxZtreeVo parent = roleMap.get(tree.getpId());// 根据父Id查询
				if (parent != null) {
					parent.getChildren().add(tree);
				}
			}
		}
		// 选中有权限码的节点及父节点
		  root.removeNoRight();
		v.setTreeData(root.getChildren());
		v.setPurviews(purids);
		
		return v;
	}

	
	
	//保存角色权限码
		@RequestMapping(value="/role/purviewSave",method=RequestMethod.POST)
		public  @ResponseBody Result savePurview(@RequestParam(required = false) Long id,
				@RequestParam(required = false) List<Long> purid,
				ModelAndView  modelAndView){
			Result result = new Result(StateDefine.FLAG_SUCCESS, true);
			moduleRolePurviewService.rolePurviewSave(id, purid);
			return result;
		}

}
